package com.orangelife.mobile.myhouse.activity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.common.sp.UserSp;
import com.curry.android.http.OrangeErrorHandler;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.common.jump.HouseJumper;
import com.orangelife.mobile.myhouse.biz.HouseHttpBiz;
import com.orangelife.mobile.util.StringUtil;
import com.orangelife.mobile.util.ToastHelper;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 户主邀请的界面
 * <p/>
 * Created by 吴格非 on 2015/11/13.
 */
public class HouseInviteActivity extends OrangeLifeBaseActivity {
    @Bind(R.id.etTel)
    EditText etTel;
    @Bind(R.id.btnInvite)
    Button btnInvite;
    @Bind(R.id.ib_title_left)
    LinearLayout ibTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    private String certificationID = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_house_invite);
        ButterKnife.bind(this);
        getIntentInfo();
        initTitle();
    }

    private void getIntentInfo() {
        certificationID = getIntent().getStringExtra("applicationID");
    }

    private void initTitle() {
        tvTitle.setText(getString(R.string.identity));
    }

    @OnClick(R.id.btnInvite)
    public void onClick() {
        requestInvite();
    }

    private void requestInvite() {
        String tel = etTel.getText().toString();
        if (StringUtil.isContainBlank(tel)) {
            ToastHelper.getInstance()._toast("手机号不能为空");
            return;
        } else if (!StringUtil.isPhone(tel)) {
            ToastHelper.getInstance()._toast("手机号格式错误");
            return;
        } else if (tel.equals(UserSp.getRegPhone())) {
            ToastHelper.getInstance()._toast("对不起，不能邀请自己");
            return;
        }
        showDialog(this);
        HouseHttpBiz.requestInvite(new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                closeDialog();
                if (OrangeErrorHandler.getInstance().isSuccess(response, "对不起，邀请失败", "")) {
                    etTel.setText("");
                    HouseJumper.jumpToTip(HouseInviteActivity.this, HouseTipActivity.TYPE_TIP_INVITE_OK, null, null, null);
                }
            }
        }, certificationID, tel);
    }
}
